var util = require('../../utils/util.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        link: '/pages/home/home',
        url: ' https://tiaonali.com/zhiwei1.jpg'
      }, {
        link: '/pages/news/index',
        url: 'https://tiaonali.com/zhiwei2.jpg'
      }
    ],
    showDialog:true,
    companyPage: {
      loading: false,
      pageNum: 0,
      now_page_no: 1,
      data: [],
      noMore: false,
    },
    currentTab: 0,
    banner: {},
    channel: {},
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    isShow: false,     //为true表示openid和userinfo都有，即已登陆
    nonet: true, 
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    util.post(app.api.getAdcarouse, { type: 1 }, function (res) {
      //获取首页轮播图
      that.setData({
        banner: res.data
      });
  
    });

    this.resetData(true);
  },
  resetData: function (isOnLoad, isappend, now_page_no) {
    var isappend = isappend === undefined ? false : true;
    var page_no = now_page_no ?  now_page_no : 1;
    var url = "";
    if (isOnLoad) {
      //如果是onload函数中执行此函数，那么resetDataForCompany要执行
      this.resetDataForCompany(app.api.companyList, page_no, isappend);
      this
    } else {
      if (this.data.currentTab == 0) {
        //最新职位
        console.log(page_no,123)
        url = app.api.companyList;
        this.resetDataForCompany(url, page_no, isappend);
      } else {
      }
    }

  },

  resetDataForCompany: function (url, now_page_no, isappend) {
    var that = this;
    var currObj = that.data.companyPage;
    that.data.companyPage++;
    util.post(url, { page: now_page_no, size: 10 }, function (res) {
      if (res.data == null) {
        wx.showToast({
          title: '正在加载中......',
          icon: 'loading',
          duration: 1200
        })
        return;
      }
      if (isappend) {
        //上拉加载更多
        that.setData({ 'companyPage.loading': false });
        now_page_no = now_page_no + 1;
        var data = [];
        for (var item in currObj.data) {
          data.push(currObj.data[item]);
        }
        for (var item in res.data.data) {
          data.push(res.data.data[item]);
        }

      } else {
        that.setData({ 'companyPage.noMore': false });
        //下拉刷新 此处停止刷新效果  重置now_page_no为1
        now_page_no = 1;
        wx.stopPullDownRefresh();
        var data = res.data.data;
        //wx.showToast({ title: '刷新了' });
      }
      that.setData({ 'companyPage.pageNum': res.data.pageNum });
      that.setData({ 'companyPage.data': data, 'companyPage.now_page_no': now_page_no });
    });
  },

  //公司详情
  companyDetailTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
   // var cityId = event.currentTarget.dataset.cityid;
    // 切换页面
    wx.navigateTo({
      url: '../company-detail/company-detail?id=' + id
    });
  },
  //点击热招职位
  clickHotPosition: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    // 切换页面
    wx.navigateTo({
      url: '../company-detail/company-detail?id=' + id + '&currentTab=1'
    });
  },
  searchInp: function (event) {
    wx.navigateTo({
      url: '/pages/search/search?cid=' + 1+'&csearch='+1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
 
    this.resetData(false);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var data =this.data.companyPage.now_page_no;
    data = data === 1?2:data
    var currObj = this.data.companyPage;
    if (currObj.loading) return;
    if (currObj.now_page_no > currObj.pageNum) {
        this.setData({ 'companyPage.noMore': true });
    }else{
      this.resetData(true,true,data);
    }

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 
  
})